package com.xdz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdz.service.CategoryBrandRelationService;
import com.xdz.utils.PageUtils;
import com.xdz.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xdz.dao.CategoryDao;
import com.xdz.entity.CategoryEntity;
import com.xdz.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        // 先查询所有的数据
        LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<CategoryEntity>()
                .eq(CategoryEntity::getShowStatus, 1);
        List<CategoryEntity> all = this.list(wrapper);
        // 进行封装
        List<CategoryEntity> listTree = all.stream()
                .filter(categoryEntity -> {
                    return categoryEntity.getParentCid() == 0;
                })
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getChildren(categoryEntity,all));
                    return categoryEntity;
                })
                .sorted((item1, item2) -> {
                    return (item1.getSort()==null ? 0:item1.getSort()) - (item2.getSort()==null?0:item2.getSort());
                })
                .collect(Collectors.toList());
        return listTree;
    }

    @Override
    public Long[] selectCatelogPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId,path);
        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public String selectCatelogPathName(Long catelogId) {
        StringBuffer sb = new StringBuffer();
        StringBuffer sbName = findParentPathName(catelogId,sb);
        return sbName.toString();
    }

    private StringBuffer findParentPathName(Long catelogId, StringBuffer sb) {
        CategoryEntity categoryEntity = this.getById(catelogId);
        sb.append(categoryEntity.getName());
        sb.append("/");
        if(categoryEntity.getParentCid()!=0){
            findParentPathName(categoryEntity.getCatId(),sb);
        }
        return sb;
    }

    @Override
    @Transactional
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())) {
            categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
        }
    }

    private List<Long> findParentPath(Long catelogId, List<Long> path) {
        path.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if(categoryEntity.getParentCid()!=0){
            findParentPath(categoryEntity.getParentCid(),path);
        }
        return path;
    }

    public List<CategoryEntity> getChildren(CategoryEntity self , List<CategoryEntity> all){
        List<CategoryEntity> collect = all.stream()
                .filter(item -> {
                    return self.getCatId() == item.getParentCid();
                })
                .map(item -> {
                    item.setChildren(getChildren(item, all));
                    return item;
                })
                .sorted((item1, item2) -> {
                    return (item1.getSort() == null ? 0:item1.getSort()) - (item2.getSort() == null ? 0:item2.getSort());
                })
                .collect(Collectors.toList());
        return collect;
    }

}